//
//  MainMenuController.m
//  AgriMobile
//
//  Created by 春名 啓作 on 11/06/21.
//  Copyright 2011 e-agri. All rights reserved.
//

#import "MainMenuController.h"


@implementation MainMenuController

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	//UIImage with resize
	UIImage* srcImg = [UIImage imageNamed:@"icon.png"];
	CGSize newSize = CGSizeMake(30.0f, 30.0f);
	UIGraphicsBeginImageContext(newSize);
	[srcImg drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
	UIImage* btnImg = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	
	UIBarButtonItem *img = [[[UIBarButtonItem alloc] initWithImage:btnImg
															 style:UIBarButtonItemStylePlain
															target:nil action:nil] retain];
	self.navigationItem.leftBarButtonItem = img;
	[img release];
	
}

- (void)viewWillAppear:(BOOL)animated {

	[self clickUpdate:nil];
}

- (IBAction)showPage:(id)sender {

	UIButton *btn = (UIButton*)sender;
	UIViewController* viewCon = nil;
	
	NSLog(@"showPage tag:%d", btn.tag);
	
	switch (btn.tag) {
		case 1:
			viewCon = [[[FriendComViewController alloc] initWithNibName:@"FriendComView" bundle:nil] retain];
			((FriendComViewController*)viewCon).actionId = @"a=page_fh_event_list";
			viewCon.title = @"参加イベントリスト";
			break;
		case 2:
			viewCon = [[[DiaryListViewController alloc] initWithNibName:@"TopicListView" bundle:nil] retain];
			viewCon.title = @"日記";
			break;
		case 3:
			//viewCon = [[[MessageListViewController alloc] initWithNibName:@"TopicListView" bundle:nil] retain];
			viewCon = [[[TopicListViewController alloc] initWithNibName:@"TopicListView" bundle:nil] retain];
			((TopicListViewController*)viewCon).topicListUrl = @"a=page_h_message_box&box=inbox";
			viewCon.title = @"受信メッセージ";
			break;
		case 4:
			viewCon = [[[FriendComViewController alloc] initWithNibName:@"FriendComView" bundle:nil] retain];
			((FriendComViewController*)viewCon).actionId = @"a=page_fh_com_list";
			viewCon.title = @"コミュニティ";
			break;
		case 5:
			viewCon = [[[HomeViewController alloc] initWithNibName:@"HomeView" bundle:nil] retain];
			viewCon.title = @"マイホーム";
			break;
		case 6:
			viewCon = [[[FriendComViewController alloc] initWithNibName:@"FriendComView" bundle:nil] retain];
			((FriendComViewController*)viewCon).actionId = @"a=page_fh_album_list";
			viewCon.title = @"マイアルバム";
			break;
		case 7:
			viewCon = [[[FriendComViewController alloc] initWithNibName:@"FriendComView" bundle:nil] retain];
			((FriendComViewController*)viewCon).actionId = @"a=page_fh_friend_list";
			viewCon.title = @"マイフレンド";
			break;
		case 9:
			viewCon = [[[ConfigViewController alloc] initWithNibName:@"ConfigView" bundle:nil] retain];
			viewCon.title = @"設定";
			break;
		default:
			NSLog(@"showPage tag:%d", btn.tag);
			break;
	}

	[super.navigationController pushViewController:viewCon animated:YES];
	//[self.view addSubview:viewCon.view];
	[viewCon release];
	
}


- (IBAction)clickUpdate:(id)sender {
	
	NSLog(@"MainMenu clickUpdate");
	
	SharedDictionary *sd = [SharedDictionary sharedDictionary];
	if(sd.data != nil)
		return;

	[actInd startAnimating];
	[NSThread detachNewThreadSelector:@selector(updateHome) toTarget:self withObject:nil];
}

- (void)updateHome {

	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	/*
	LoadingViewController *loadingViewController = [[LoadingViewController alloc] initWithNibName:@"LoadingView" bundle:nil];
	[self.view addSubview:loadingViewController.view];
	[super.view bringSubviewToFront:loadingViewController.view];

	 [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
	 UIActivityIndicatorView *ai = [[UIActivityIndicatorView alloc] init];
	 ai.frame = CGRectMake(0, 0, 50, 50);
	 ai.center = self.view.center;
	 ai.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
	 [self.view addSubview:ai];
	 [self.view bringSubviewToFront:ai];
	 [ai startAnimating];
	 */

	HttpMethod *http = [HttpMethod alloc];
	NSUserDefaults *udef = [NSUserDefaults standardUserDefaults];
	if ([udef stringForKey:@"email"] != nil) {
		[http doGet:[[NSString alloc]initWithFormat:@"%@/wap?login=%@&pwd=%@", URLRoot,
					 [udef stringForKey:@"email"], [udef stringForKey:@"password"]]];
	}
	
	SharedDictionary *sd = [SharedDictionary sharedDictionary];
	sd.data = [[http doGet:[[NSString alloc]initWithFormat:@"%@/wap?a=top", URLRoot]] JSONValue];
	NSLog(@"maps = %@", sd.data);
	
	/*
	 [ai stopAnimating];  // アニメーションを停止させたい時に呼ぶ
	 [ai removeFromSuperview];
	 [ai release];
	 [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
	
	[loadingViewController.view removeFromSuperview];
	[loadingViewController release];
	 */
	[self performSelectorOnMainThread:@selector(updateHomeDone) withObject:nil waitUntilDone:NO];
	[pool release];
	[NSThread exit];
}

- (void)updateHomeDone {
	[actInd stopAnimating];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
